package com.health.ts.controller.scheduling;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.dao.register.SourceMapper;
import com.health.ts.entity.register.Source;
import com.health.ts.entity.scheduling.Scheduling;
import com.health.ts.entity.scheduling.Vscheduling;
import com.health.ts.entity.sysmanage.SysDepartment;
import com.health.ts.entity.sysmanage.SysDevice;
import com.health.ts.entity.sysmanage.SysUser;
import com.health.ts.service.scheduling.SchedulingService;
import com.health.ts.service.scheduling.VschedulingService;
import com.health.ts.utils.SessionUserUtil;
/**
 * <p>
 *  排班管理
 * </p>
 *
 * @since 2017-07-05
 */
@RestController
@RequestMapping(value = "/api/scheduling",produces= BaseController.JSON_UTF8)
public class SchedulingController extends BaseController{
	
	@Autowired
	SchedulingService schedulingService;
	
	@Autowired
	VschedulingService vSchedulingService;
	
	@Autowired
	SourceMapper sourceMapper;
	
	/**  
	* @Title: queryScheduling  
	* @Description: 查询管理排班列表(不带分页)
	* @param @param param
	* @return String 返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/manage/list", method=RequestMethod.GET)
	public ApiResult listManageScheduling(Vscheduling param,String start_date,String end_date) {
		QueryWrapper<Vscheduling> wrapper = new QueryWrapper<Vscheduling>();
		//默认条件 用户所属医院排班
		wrapper.eq("schedu_hid", SessionUserUtil.getUserHosId());
		//排班类型
		String schedu_type =param.getSchedu_type();
		if (StringUtils.isNotEmpty(schedu_type)){
			wrapper.eq("schedu_type", schedu_type);
		}
		//排班科室ID
		String schedu_did =param.getSchedu_did();;
		if (StringUtils.isNotEmpty(schedu_did)){
			wrapper.eq("schedu_did", schedu_did);
		}
		//排班医生ID
		String doc_id =param.getDoc_id();
		if (StringUtils.isNotEmpty(doc_id)){
			wrapper.eq("doc_id", doc_id);
		}
		//排班设备ID
		String device_id =param.getDevice_id();
		if (StringUtils.isNotEmpty(device_id)){
			wrapper.eq("device_id", device_id);
		}
		//排班日期区间
		if (StringUtils.isNotEmpty(start_date)){
			wrapper.between("schedu_date", start_date, end_date); 
		}
		wrapper.orderByDesc("update_time");
		List<Vscheduling> resultList = vSchedulingService.list(wrapper);
		return ApiResult.success(resultList);
	}
	
	/**  
	* @Title: queryScheduling  
	* @Description: 查询显示排班列表(不带分页)
	* @param @param param
	* @return String 返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/show/list", method=RequestMethod.GET)
	public ApiResult listScheduling(Vscheduling param,String start_date,String end_date) {
		QueryWrapper<Vscheduling> wrapper = new QueryWrapper<Vscheduling>();
		//排班类型
		String schedu_type =param.getSchedu_type();
		if (StringUtils.isNotEmpty(schedu_type)){
			wrapper.eq("schedu_type", schedu_type);
		}
		//排班医院ID
		String schedu_hid =param.getSchedu_hid();;
		if (StringUtils.isNotEmpty(schedu_hid)){
			wrapper.eq("schedu_hid", schedu_hid);
		}
		//排班科室ID
		String schedu_did =param.getSchedu_did();;
		if (StringUtils.isNotEmpty(schedu_did)){
			wrapper.eq("schedu_did", schedu_did);
		}
		//排班医生ID
		String doc_id =param.getDoc_id();
		if (StringUtils.isNotEmpty(doc_id)){
			wrapper.eq("doc_id", doc_id);
		}
		//排班设备ID
		String device_id =param.getDevice_id();
		if (StringUtils.isNotEmpty(device_id)){
			wrapper.eq("device_id", device_id);
		}
		//排班日期区间
		if (StringUtils.isNotEmpty(start_date)){
			wrapper.between("schedu_date", start_date, end_date); 
		}
		wrapper.orderByDesc("update_time");
		List<Vscheduling> resultList = vSchedulingService.list(wrapper);
		for(Vscheduling temp : resultList){
			String scheduId = temp.getId();
			QueryWrapper<Source> sourceWrapper = new QueryWrapper<Source>();
			sourceWrapper.eq("pbid", scheduId);
			sourceWrapper.eq("is_used", "0");
			//设置可用号源数
			temp.setUsable_source_num(sourceMapper.selectCount(sourceWrapper).intValue());
		}
		return ApiResult.success(resultList);
	}
	
	/**  
	* @Title: queryScheduling  
	* @Description: 查询排班分页列表
	* @param @param param
	* @return String 返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/pager", method=RequestMethod.GET)
	public ApiResult listSchedulingPager(@RequestBody FrontPage param) {
		QueryWrapper<Vscheduling> wrapper = new QueryWrapper<Vscheduling>();
		//默认条件 用户所属医院排班
		wrapper.eq("schedu_hid", SessionUserUtil.getUserHosId());
		//排班类型
		String schedu_type =getParam(param,"schedu_type");
		if (StringUtils.isNotEmpty(schedu_type)){
			wrapper.eq("schedu_type", schedu_type);
		}
		//排班科室ID
		String schedu_did =getParam(param,"schedu_did");
		if (StringUtils.isNotEmpty(schedu_did)){
			wrapper.eq("schedu_did", schedu_did);
		}
		//排班医生ID
		String doc_id =getParam(param,"doc_id");
		if (StringUtils.isNotEmpty(doc_id)){
			wrapper.eq("doc_id", doc_id);
		}
		//排班设备ID
		String device_id =getParam(param,"device_id");
		if (StringUtils.isNotEmpty(device_id)){
			wrapper.eq("device_id", device_id);
		}
		//排班日期区间
		String start_date =getParam(param,"start_date");
		String end_date =getParam(param,"end_date");
		if (StringUtils.isNotEmpty(start_date)){
			wrapper.between("schedu_date", start_date, end_date); 
		}
		wrapper.orderByDesc("update_time");

		Page<Vscheduling> pageList = vSchedulingService.page(param.getPagePlus(), wrapper);
		CustomPage<Vscheduling> customPage = new CustomPage<Vscheduling>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**  
	* @Title: getScheduling  
	* @Description: TODO(查询单个排班)  
	* @param @param scheduId
	* @return String  返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/{scheduId}", method=RequestMethod.GET)
	public ApiResult getScheduling(@PathVariable String scheduId) {
		Scheduling scheduling = schedulingService.getScheduling(scheduId);
		return ApiResult.success(scheduling);
	}
	
	/**  
	* @Title: addScheduling  
	* @Description: TODO(新增排班)  
	* @param @param scheduling
	* @return String  返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="", method=RequestMethod.POST)
	public ApiResult addScheduling(@RequestBody Scheduling scheduling) {
		String scheduId=schedulingService.addScheduling(scheduling);
		return ApiResult.success(scheduId);
	}
	
	/**  
	* @Title: deleteScheduling  
	* @Description: TODO(删除排班)  
	* @param @param scheduId
	* @return String  返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/{scheduId}", method=RequestMethod.DELETE)
	public ApiResult deleteScheduling(@PathVariable String scheduId) {
		schedulingService.deleteScheduling(scheduId);
		return ApiResult.success();
	}
	
	/**  
	* @Title: uopdateScheduling  
	* @Description: 更新排班 
	* @param @param schedulingId
	* @param @param scheduling
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/{scheduId}", method=RequestMethod.PUT)
	public ApiResult uopdateScheduling(@PathVariable String scheduId,@RequestBody Scheduling scheduling) {
		schedulingService.updateScheduling(scheduId,scheduling);
		return ApiResult.success();
	}
	
	/**  
	* @Title: getScheduDepts  
	* @Description: TODO(查询排班科室)  
	* @throws  
	*/ 
	@RequestMapping(value="/depts", method=RequestMethod.GET)
	public ApiResult getScheduDepts() {
		List<SysDepartment> scheduDepts = schedulingService.getScheduDepts();
		return ApiResult.success(scheduDepts);
	}
	
	/**  
	* @Title: getScheduDoctors  
	* @Description: TODO(查询排班医生)  
	* @throws  
	*/ 
	@RequestMapping(value="/doctors", method=RequestMethod.GET)
	public ApiResult getScheduDoctors(String department_id ) {
		List<SysUser> scheduDoctors = schedulingService.getScheduDoctors(department_id);
		return ApiResult.success(scheduDoctors);
	}
	
	/**  
	* @Title: getScheduDevices
	* @Description: TODO(查询排班设备)  
	* @throws  
	*/ 
	@RequestMapping(value="/devices", method=RequestMethod.GET)
	public ApiResult getScheduDevices(String department_id ) {
		List<SysDevice> scheduDevices = schedulingService.getScheduDevices(department_id);
		return ApiResult.success(scheduDevices);
	}
}
